package co.edu.univalle.persistencia;

import co.edu.univalle.modelo.Clientes;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DILOVE
 */
@Stateless
public class ClienteDAO implements IClienteDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void registrar(Clientes cliente) {
        try {
            entityManager.persist(cliente);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Clientes cliente) {
        try{
            entityManager.merge(cliente);
        }catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public void eliminar(Clientes cliente) {
        try{
            entityManager.remove(cliente);
        }catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public List<Clientes> consultar() {
        try{
            String jpql = "select c from Clientes c";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        }catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public Clientes consultarPorCedula(Long cedula) {
        try{
            return entityManager.find(Clientes.class, cedula);
        }catch(RuntimeException e){
            throw e;
        }
    }
}
